﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AnyOffice.Components;
using AnyOffice.DAL;
using AnyOffice.Model;
using Microsoft.ApplicationBlocks.Data;

namespace AnyOffice.general.work_plan.show
{
    public partial class query : ModulePage
    {
        public string GetStatusDesc(int status)
        {
            switch (status)
            {
                case 0:
                    return "<font color=\"red\"><b>未开始</font>";

                case 1:
                    return "<font color=\"blue\"><b>进行中</font>";

                case 2:
                    return "<font color=\"green\"><b>完成</font>";
            }
            return "";
        }


        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.Page.IsPostBack)
            {
                DataTable table = new PlanTypeDao().SelectAll(base.Conn);
                this.ddlTypeId.Items.Add(new ListItem("全部类型", ""));
                if (table.Rows.Count > 0)
                {
                    for (int i = 0; i < table.Rows.Count; i++)
                    {
                        ListItem item = new ListItem(table.Rows[i]["TypeName"].ToString(), table.Rows[i]["TypeId"].ToString());
                        this.ddlTypeId.Items.Add(item);
                    }
                }
                  

            }
            if (this.Page.IsPostBack)
            {
                this.pnlMain.Visible = false;
                this.pnlLater.Visible = true;
                ArrayList list = new ArrayList();
                string commandText = "SELECT ";
                commandText = ((((((commandText + "\t[Plan_Work].[PlanId]," + "\t[Plan_Work].[Name],") + "\t[Plan_Work].[BeginDate]," + "\t[Plan_Work].[EndDate],") + "\t[Plan_Type].[TypeName]," + "\tdbo.GetDeptNames([Plan_Work].[ToId]) AS ToName,") + "\t[Plan_Work].[CompletedPercent]," + "\t[Plan_Work].[Status],") + "\t[Users].[UserName] " + "FROM ") + "\t[dbo].[Plan_Work], [dbo].[Plan_Type], [dbo].[Users] " + "WHERE ") + "\t([Plan_Work].[TypeId] = [Plan_Type].[TypeId]) " + "\tAND ([Plan_Work].[Creator] = [Users].[UserId]) ";
                if (this.txtName.Value != string.Empty)
                {
                    commandText = commandText + " AND ([Plan_Work].[Name] LIKE '%' + @Name + '%') ";
                    list.Add(new object[] { "@Name", this.txtName.Value });
                }
                if (this.txtContent.Value != string.Empty)
                {
                    commandText = commandText + " AND ([Plan_Work].[Content] LIKE '%' + @Content + '%') ";
                    list.Add(new object[] { "@Content", this.txtContent.Value });
                }
                if (this.txtBeginDate.Value != string.Empty)
                {
                    commandText = commandText + " AND ([Plan_Work].[BeginDate] >= @BeginDate) ";
                    list.Add(new object[] { "@BeginDate", Convert.ToDateTime(this.txtBeginDate.Value + " 00:00:00") });
                }
                if (this.txtEndDate.Value != string.Empty)
                {
                    commandText = commandText + " AND ([Plan_Work].[EndDate] <= @EndDate) ";
                    list.Add(new object[] { "@EndDate", Convert.ToDateTime(this.txtEndDate.Value + " 23:59:59") });
                }
                if (this.ddlTypeId.SelectedValue != string.Empty)
                {
                    commandText = commandText + " AND ([Plan_Work].[TypeId] = @TypeId) ";
                    list.Add(new object[] { "@TypeId", Convert.ToInt32(this.ddlTypeId.SelectedValue) });
                }
                if (this.ParticipatorId.Value != string.Empty)
                {
                    commandText = commandText + " AND (CHARINDEX(',' + @ParticipatorId + ',',',' + CAST([Plan_Work].[Participator] AS nvarchar(2000)) + ',') > 0) ";
                    list.Add(new object[] { "@ParticipatorId", this.ParticipatorId.Value });
                }
                if (this.ManagerId.Value != string.Empty)
                {
                    commandText = commandText + " AND (CHARINDEX(',' + @ManagerId + ',',',' + CAST([Plan_Work].[Manager] AS nvarchar(2000)) + ',') > 0) ";
                    list.Add(new object[] { "@ManagerId", this.ManagerId.Value });
                }
                if (this.txtRemark.Value != string.Empty)
                {
                    commandText = commandText + " AND ([Plan_Work].[Remark] LIKE '%' + @Remark + '%') ";
                    list.Add(new object[] { "@Remark", this.txtRemark.Value });
                }
                if (this.ToId.Value != string.Empty)
                {
                    if (this.ToId.Value == "ALL_DEPT")
                    {
                        commandText = commandText + " AND ([Plan_Work].[ToId] LIKE 'ALL_DEPT') ";
                    }
                    else
                    {
                        string str2 = this.ToId.Value;
                        if (str2.EndsWith(","))
                        {
                            str2 = str2.Substring(0, str2.Length - 1);
                        }
                        string[] strArray = str2.Split(new char[] { ',' });
                        if (strArray.Length > 0)
                        {
                            commandText = commandText + "AND (";
                            for (int k = 0; k < strArray.Length; k++)
                            {
                                if (k != 0)
                                {
                                    commandText = commandText + " OR ";
                                }
                                commandText = commandText + " (CHARINDEX('," + strArray[k] + ",',',' + CAST([Plan_Work].[ToId] AS nvarchar(2000)) + ',') > 0) ";
                            }
                            commandText = commandText + ")";
                        }
                    }
                }
                ConfigManager manager = new ConfigManager();
                UserCache cache = new UserCache();
                if ((((cache.PrivId == manager.AdministratorPrivId) ? 1 : 0) == 0) && (cache.PostPriv != "1"))
                {
                    commandText = ((commandText + " AND (" + "(CAST([Plan_Work].[ToId] AS nvarchar(2000)) = 'ALL_DEPT') ") + "OR (CHARINDEX('," + base.User.Identity.Name + ",',',' + CAST([Plan_Work].[Participator] AS nvarchar(2000)) + ',') > 0) ") + "OR (CHARINDEX('," + cache.DeptId.ToString() + ",',',' + CAST([Plan_Work].[ToId] AS nvarchar(2000)) + ',') > 0) ";
                    string postDept = cache.PostDept;
                    if ((cache.PostPriv == "2") && (postDept != string.Empty))
                    {
                        if (postDept.EndsWith(","))
                        {
                            postDept = postDept.Substring(0, postDept.Length - 1);
                        }
                        string[] strArray2 = postDept.Split(new char[] { ',' });
                        if (strArray2.Length > 0)
                        {
                            for (int m = 0; m < strArray2.Length; m++)
                            {
                                commandText = commandText + "OR (CHARINDEX('," + strArray2[m] + ",',',' + CAST([Plan_Work].[ToId] AS nvarchar(2000)) + ',') > 0) ";
                            }
                        }
                    }
                    commandText = commandText + " )";
                }
                commandText = commandText + " ORDER BY [Plan_Work].[PlanId] DESC";
                SqlParameter[] commandParameters = new SqlParameter[list.Count];
                for (int j = 0; j < list.Count; j++)
                {
                    object[] objArray9 = (object[])list[j];
                    commandParameters[j] = new SqlParameter(objArray9[0].ToString(), objArray9[1]);
                }
                DataTable table2 = SqlHelper.ExecuteDataset(base.Conn, CommandType.Text , commandText, commandParameters).Tables[0];
                if (table2.Rows.Count > 0)
                {
                    this.rptWorkPlan.DataSource = table2;
                    this.rptWorkPlan.DataBind();
                }
                else
                {
                    this.litMessage.Text = base.ShowMessage("无符合条件的项目");
                }
            }
        }

 

    }
}
